package com.shuidi.oilactivity.activity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.androidkun.xtablayout.XTabLayout;
import com.shuidi.oilactivity.R;
import com.shuidi.oilactivity.adapter.SmallCustomerAdapter;
import com.shuidi.oilactivity.bean.Customer;
import com.shuidi.oilactivity.utils.ViewPaperAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class AllCustomerActivity extends BaseActivity {

    @BindView(R.id.top_left_btn)
    ImageButton topLeftBtn;
    @BindView(R.id.title_bar)
    TextView titleBar;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tabs)
    XTabLayout tabLayout;
    @BindView(R.id.view)
    ViewPager mViewPager;


    private LayoutInflater mInflater;

    private View smallView;
    private View bigView;
    private List<String> mTitleList = new ArrayList<>();
    private List<View> mViewList = new ArrayList<>();

    private List<Customer> customerList = new ArrayList<>();
    private RecyclerView smallRecyclerView;
    private RecyclerView bigRecyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_customer);
        ButterKnife.bind(this);
        titleBar.setText("全部客户");
        init();

        if (ContextCompat.checkSelfPermission(AllCustomerActivity.this, Manifest.permission.CALL_PHONE)!= PackageManager.PERMISSION_GRANTED){
            //如果不同意，就去请求权限   参数1：上下文，2：权限，3：请求码
            ActivityCompat.requestPermissions(AllCustomerActivity.this,new String []{Manifest.permission.CALL_PHONE},1);
        }
    }

    private void init() {
        mInflater = LayoutInflater.from(this);
        smallView = mInflater.inflate(R.layout.all_customer, null);
        smallViewAdapter();
        bigView = mInflater.inflate(R.layout.all_customer, null);
        bigViewAdapter();
        mViewList.add(smallView);
        mViewList.add(bigView);
        mTitleList.add("小微客户");
        mTitleList.add("企业客户");
        tabLayout.setTabMode(TabLayout.MODE_FIXED);//设置tab模式，当前为系统默认模式
        tabLayout.addTab(tabLayout.newTab().setText(mTitleList.get(0)));//添加选项卡
        tabLayout.addTab(tabLayout.newTab().setText(mTitleList.get(1)));
        ViewPaperAdapter mAdapter = new ViewPaperAdapter(mViewList, mTitleList);
        mViewPager.setAdapter(mAdapter);//给ViewPager设置适配器
        tabLayout.setupWithViewPager(mViewPager);  //将TabLayout和ViewPager关联起来。
        tabLayout.setTabsFromPagerAdapter(mAdapter);//给Tabs设置适配器
    }

    @OnClick(R.id.top_left_btn)
    public void onViewClicked() {
        finish();
    }

    private void smallViewAdapter(){
        smallRecyclerView = (RecyclerView)smallView.findViewById(R.id.recycler);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        smallRecyclerView.setLayoutManager(layoutManager);

        BmobQuery<Customer> bmobQuery = new BmobQuery<Customer>();
        bmobQuery.addWhereEqualTo("userType", 1);
        bmobQuery.findObjects(new FindListener<Customer>() {
            @Override
            public void done(List<Customer> list, BmobException e) {
                if(e==null){
                    customerList=list;
                    SmallCustomerAdapter smallCustomerAdapter = new SmallCustomerAdapter(customerList);
                    smallRecyclerView.setAdapter(smallCustomerAdapter);
                }else{
                    Log.i("bmob","失败："+e.getMessage()+","+e.getErrorCode());
                }
            }
        });
    }

    private void bigViewAdapter(){
        bigRecyclerView = (RecyclerView)bigView.findViewById(R.id.recycler);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        bigRecyclerView.setLayoutManager(layoutManager);

        BmobQuery<Customer> bmobQuery = new BmobQuery<Customer>();
        bmobQuery.addWhereEqualTo("userType", 2);
        bmobQuery.findObjects(new FindListener<Customer>() {
            @Override
            public void done(List<Customer> list, BmobException e) {
                if(e==null){
                    customerList=list;
                    SmallCustomerAdapter smallCustomerAdapter = new SmallCustomerAdapter(customerList);
                    bigRecyclerView.setAdapter(smallCustomerAdapter);
                }else{
                    Log.i("bmob","失败："+e.getMessage()+","+e.getErrorCode());
                }
            }
        });
    }
}
